﻿using System;
using System.IO;

namespace OpenLS.Spreadsheet.Formats.Biff.Records
{
    struct RefU
    {
        public override string ToString()
        {
            return string.Format("{0} {1} {2} {3}", rwFirst.ToString(), rwLast, colFirst, colLast);
        }
        private ushort rwFirst;
        private ushort rwLast;
        private byte colFirst;
        private byte colLast;
        public static explicit operator RangeAddress(RefU refU)
        {
            return new RangeAddress(new CellAddress(refU.rwFirst, refU.colFirst), new CellAddress(refU.rwLast, refU.colLast));
        }
        public static explicit operator RefU(RangeAddress refU)
        {
            return new RefU()
                       {
                           rwFirst = (ushort) refU.TopLeft.Row,
                           rwLast = (ushort) refU.BottomRight.Row,
                           colFirst = (byte) refU.TopLeft.Column,
                           colLast = (byte) refU.BottomRight.Column
                       };
        }

        public void Read(BinaryReader reader)
        {
            rwFirst = reader.ReadUInt16();
            rwLast = reader.ReadUInt16();
            colFirst = reader.ReadByte();
            colLast = reader.ReadByte();

        }

        public void Write(BinaryWriter writer)
        {
            writer.Write(rwFirst);
            writer.Write(rwLast);
            writer.Write(colFirst);
            writer.Write(colLast);
        }
    }
}